"""
系统配置模块
集中管理所有配置项和常量
"""

import os
from pathlib import Path
from loguru import logger

# ======================= 系统配置 =======================
# 本地存储配置
LOCAL_STORAGE_ROOT = os.environ.get("LOCAL_STORAGE_ROOT", "/data/dicom_files")
LOCAL_STORAGE_PATH = Path(LOCAL_STORAGE_ROOT)

# Triton推理服务器配置
TRITON_SERVER_URL = os.environ.get("TRITON_SERVER_URL", "192.168.1.249:8010")

# Triton模型配置映射表
MODEL_CONFIGS = {
    "stenosis_detection": "stenosis_model_v1",     # 狭窄检测模型
    "lca_f_segmentation": "lca_f_segment_v1",      # 左冠前降支分割模型
    "lca_ls_segmentation": "lca_ls_segment_v1",    # 左冠侧支分割模型
    "rca_lao_segmentation": "rca_lao_segment_v1",  # 右冠LAO位分割模型
    "occlusion_detection": "occlusion_model_v1"    # 闭塞检测模型
}

# ======================= 缓存配置 =======================
# Triton缓存配置
TRITON_CACHE_SIZE = int(os.environ.get("TRITON_CACHE_SIZE", "10"))
TRITON_CACHE_TTL = int(os.environ.get("TRITON_CACHE_TTL", "300"))
TRITON_MAX_CONCURRENT = int(os.environ.get("TRITON_MAX_CONCURRENT", "4"))

# 文件系统缓存配置
FS_CACHE_REFRESH_INTERVAL = int(os.environ.get("FS_CACHE_REFRESH_INTERVAL", "60"))

# 任务管理配置
TASK_TTL = int(os.environ.get("TASK_TTL", "3600"))  # 1小时
MAX_TASKS = int(os.environ.get("MAX_TASKS", "1000"))

# ======================= 日志配置 =======================
def setup_logging():
    """设置日志配置"""
    log_level = os.environ.get("LOG_LEVEL", "INFO")
    logger.remove()  # 移除默认配置
    logger.add(
        sink=lambda msg: print(msg, end=""),
        level=log_level,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
    )

# ======================= 初始化配置 =======================
def log_configuration():
    """输出配置信息到日志"""
    logger.info(f"本地存储目录: {LOCAL_STORAGE_PATH}")
    logger.info(f"Triton推理服务地址: {TRITON_SERVER_URL}")
    logger.info(f"缓存配置 - 大小: {TRITON_CACHE_SIZE}, TTL: {TRITON_CACHE_TTL}秒")
    logger.info(f"任务配置 - TTL: {TASK_TTL}秒, 最大数量: {MAX_TASKS}")

# 应用启动时调用
setup_logging() 